package com.lyt.springboot.controller;

import com.lyt.springboot.domain.Car;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

import java.util.*;
import java.util.stream.Collectors;

public class MyTestController {
    @Test
    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }

        }
        return new int[0];
    }

    @Test
    public int removeDuplicates(int[] nums) {
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (!hashMap.containsKey(nums[i])) {
                hashMap.put(nums[i], i);
            }
        }
        return hashMap.size();
    }

    @Test
    public int removeElement(int[] nums, int val) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val && !map.containsKey(nums[i])) {
                map.put(nums[i], i);
            }
        }
        return map.size();
    }

    @Test
    public int searchInsert(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]==target) {
                return i;
            }
            if(target<nums[0]) {
               return 0;
            }else if(target>nums[nums.length-1]){
                return nums.length;
            }else if(nums[i]<target && nums[i+1]>target){
                return i+1;
            }
        }
        return 0;
    }


    @Test
    public int singleNumber(int[] nums) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (!map.containsKey(nums[i])) {
              map.put(nums[i],1);
            }else {
                map.remove(nums[i],map.get(nums[i]+1));
            }
        }
        List<Map.Entry<Integer,Integer>> list = new ArrayList<>(map.entrySet());
        List<Integer> list1 = map.entrySet().stream().map(Map.Entry::getKey).collect(Collectors.toList());
        System.out.println(list1.get(0));
        System.out.println(list.get(0).getKey());
        return  list.get(0).getKey();
    }

    public static void main(String[] args) {
//        MyTestController c = new MyTestController();
//        int[] nums = {3, 2, 2,3,5};
//        c.singleNumber(nums);


        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.remove(0);
        System.out.println(list);
    }
    @Test
    public void test0() {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        factory.addBeanPostProcessor(new InstantiationAwareBeanPostProcessor() {
            @Override
            public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
                if(beanClass == Car.class) {
                   Car car = new Car();
                   car.setName("牛逼");
                   return car;
                }
                return null;
            }
        });
//        factory.registerBeanDefinition();
    }

    // 二叉树节点定义
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    @Test
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            int levelSize = queue.size();
            List<Integer> level = new ArrayList<>();

            for (int i = 0; i < levelSize; i++) {
                TreeNode node = queue.poll();
                level.add(node.val);

                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            result.add(level);
        }

        return result;
    }

    @Test
    public void testLevelOrder() {
        // 构建测试用例
        //     3
        //    / \
        //   9  20
        //     /  \
        //    15   7
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);

        List<List<Integer>> result = levelOrder(root);
        System.out.println("层序遍历结果: " + result);
        // 预期输出: [[3], [9, 20], [15, 7]]
    }
}